package com.sw.paisong.service.impl;

import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import com.sw.paisong.dao.StaffDao;
import com.sw.paisong.entity.StaffEntity;
import com.sw.paisong.service.StaffService;
import com.sw.paisong.util.UUIDUtil;
import com.sw.paisong.vo.JsonVO;
import com.sw.paisong.vo.SessionVO;
import com.sw.paisong.vo.StaffFormVO;
import com.sw.paisong.vo.mob.HandshakeFormVO;
import com.sw.paisong.vo.mob.HandshakeResVO;

@Service
public class StaffServiceImpl implements StaffService {

	private static final Logger logger = LoggerFactory.getLogger(StaffServiceImpl.class);

	@Autowired
	StaffDao staffDao;

	@Override
	public List<StaffEntity> getStaffList() {
		return staffDao.findAll();
	}

	@Override
	public Page<StaffEntity> getStaffPage(PageRequest pageRequest) {
		return staffDao.findAll(pageRequest);
	}

	@Override
	public JsonVO<Object> addStaffs(Collection<StaffEntity> staffs, SessionVO session) {
		JsonVO<Object> json = new JsonVO<Object>();
		boolean isSuccess = true;
		String message = "";
		Map<String, StaffEntity> staffsMap = new HashMap<String, StaffEntity>();
		if (staffs != null && staffs.size() > 0) {
			for (StaffEntity staff : staffs) {
				StaffEntity temp = staffDao.findByNum(staff.getNum());
				if (temp != null) {
					staff.setId(temp.getId());
				} else {
					staff.setId(UUIDUtil.getUUIDKey());
				}
				staff.setCreateAt(new Date());
				staff.setCreateBy(session.getId());
				staffsMap.put(staff.getNum(), staff);
			}
		}
		Set<StaffEntity> staffSet = new HashSet<StaffEntity>();
		for (Entry<String, StaffEntity> staffMapEntry : staffsMap.entrySet()) {
			staffSet.add(staffMapEntry.getValue());
		}
		try {

			staffDao.save(staffSet);
		} catch (Exception ex) {
			logger.error(ex.toString());
			message = "系统异常";
			isSuccess = false;
		}
		json.setSuccess(isSuccess);
		json.setMessage(message);
		return json;
	}

	@Override
	public JsonVO<Object> addStaff(StaffFormVO staffFormVO, SessionVO session) {
		JsonVO<Object> json = new JsonVO<Object>();
		boolean isSuccess = true;
		String message = "新增成功！";
		StaffEntity temp = staffDao.findByNum(staffFormVO.getStaffNum());
		if (temp != null) {
			isSuccess = false;
			message = "对不起，该工号已经存在了！";
		}
		if (isSuccess) {
			try {
				staffFormVO.setStaffId(UUIDUtil.getUUIDKey());
				StaffEntity staff = new StaffEntity(staffFormVO);
				staff.setCreateAt(new Date());
				staff.setCreateBy(session.getId());
				staffDao.save(staff);
			} catch (Exception ex) {
				logger.error(ex.toString());
				message = "系统异常";
				isSuccess = false;
			}
		}
		json.setSuccess(isSuccess);
		json.setMessage(message);
		return json;

	}

	@Override
	public JsonVO<Object> alterStaff(StaffFormVO staffFormVO, SessionVO session) {
		JsonVO<Object> json = new JsonVO<Object>();
		boolean isSuccess = true;
		String message = "更新成功！";

		StaffEntity temp = staffDao.findByNum(staffFormVO.getStaffNum());
		if (temp != null && !temp.getId().equals(staffFormVO.getStaffId())) {
			isSuccess = false;
			message = "对不起，该工号已经存在了！";
		}
		if (isSuccess) {
			try {
				StaffEntity staff = staffDao.findOne(staffFormVO.getStaffId());
				// StaffEntity staff = new StaffEntity(staffFormVO);
				staff.setName(staffFormVO.getStaffName());
				staff.setNum(staffFormVO.getStaffNum());
				staff.setPhone(staffFormVO.getStaffPhone());
				staff.setVPhone(staffFormVO.getStaffVPhone());
				staff.setRemarks(staffFormVO.getStaffRemarks());
				staff.setUpdateAt(new Date());
				staff.setUpdateBy(session.getId());
				staffDao.save(staff);
			} catch (Exception ex) {
				logger.error(ex.toString());
				message = "系统异常";
				isSuccess = false;
			}
		}
		json.setSuccess(isSuccess);
		json.setMessage(message);
		return json;
	}

	@Override
	public JsonVO<Object> removeStaff(String id) {
		JsonVO<Object> json = new JsonVO<Object>();
		boolean isSuccess = true;
		String message = "";
		try {
			staffDao.delete(id);
		} catch (Exception ex) {
			logger.error(ex.toString());
			message = "系统异常！";
			isSuccess = false;
		}
		json.setSuccess(isSuccess);
		json.setMessage(message);
		return json;
	}

	@Override
	public JsonVO<Object> removeStaffsByIds(String[] ids) {
		JsonVO<Object> json = new JsonVO<Object>();
		boolean isSuccess = true;
		String message = "";
		Set<StaffEntity> staffs = new HashSet<StaffEntity>();
		for (String id : ids) {
			StaffEntity temp = staffDao.findOne(id);
			if (temp != null) {
				staffs.add(temp);
			}
		}
		try {
			staffDao.delete(staffs);
		} catch (Exception ex) {
			logger.error(ex.toString());
			message = "系统异常！";
			isSuccess = false;
		}
		json.setMessage(message);
		json.setSuccess(isSuccess);
		return json;
	}

	@Override
	public JsonVO<HandshakeResVO> handshake(HandshakeFormVO handshakeForm) {
		StaffEntity staff = staffDao.findByNum(handshakeForm.getStaffNum());
		if (staff != null) {
			staff.setName(handshakeForm.getUsername());
			staff.setPhone(handshakeForm.getPhone());
			staff.setVPhone(handshakeForm.getVPhone());
			staff.setUpdateAt(new Date());
			staff.setUpdateBy(staff.getId());
		} else {
			String id = UUIDUtil.getUUIDKey();
			staff = new StaffEntity();
			staff.setId(id);
			staff.setName(handshakeForm.getUsername());
			staff.setPhone(handshakeForm.getPhone());
			staff.setVPhone(handshakeForm.getVPhone());
			staff.setNum(handshakeForm.getStaffNum());
			staff.setCreateAt(new Date());
			staff.setCreateBy(id);
		}
		staffDao.save(staff);
		JsonVO<HandshakeResVO> json = new JsonVO<HandshakeResVO>();
		json.setSuccess(true);
		json.setMessage("绑定成功");
		HandshakeResVO handshakeResVO = new HandshakeResVO();
		handshakeResVO.setToken(staff.getId());
		json.setData(handshakeResVO);
		return json;
	}
}
